package com.iwunu.gateway.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
/**
 * 普通短信发送
 * @author kinfeng
 */
public class SmsTxSend {
	
	public static int sendSms(String phone, ArrayList<String> params) throws Exception{
		String secretId = Messageproperties.SecretId;  
		String secretKey = Messageproperties.SecretKey;  
	    int appid = Messageproperties.appid;       
        // 短信应用SDK AppKey        
        String appkey = Messageproperties.appkey;        
        // 短信模板ID，需要在短信应用中申请       
        int templateId = Messageproperties.templateId ;        
        // 签名，使用的是`签名内容`，而不是`签名ID`        
        String smsSign = Messageproperties.smsSign;     
	    try {
            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setReqMethod("POST");
            httpProfile.setConnTimeout(60);
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            req.setSmsSdkAppid(appid+"");
            req.setSign(smsSign);

            req.setTemplateID(templateId+"");
            String[] phoneNumbers ={Messageproperties.ch+phone};
            req.setPhoneNumberSet(phoneNumbers);
            int size=params.size();  
            String[] array = (String[])params.toArray(new String[size]);  
            req.setTemplateParamSet(array);
            SendSmsResponse res = client.SendSms(req);
            SendStatus[] result = res.getSendStatusSet();
           List<SendStatus> code = Arrays.asList(result);
           if(code.get(0).getCode().toUpperCase().equals("OK")){
        	   return 0;    
           }
           System.out.println(SendSmsResponse.toJsonString(res));
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
	        return 1;    
	    }
	public static void main(String[] args) {
			String secretId = Messageproperties.SecretId;  
			String secretKey = Messageproperties.SecretKey;  
		    int appid = Messageproperties.appid;       
	        // 短信应用SDK AppKey        
	        String appkey = Messageproperties.appkey;        
	        // 短信模板ID，需要在短信应用中申请       
	        int templateId = Messageproperties.templateId ;        
	        // 签名，使用的是`签名内容`，而不是`签名ID`        
	        String smsSign = Messageproperties.smsSign;     
			ArrayList<String> send = new ArrayList<String>();
			send.add("1002");
			send.add("123456");
			send.add("2022-07-17 00:00:00");
			send.add("2022-07-28 00:00:00");
		    String phone = "13751166502"; //手机号码,注意如果是国内的手机号码需要加86不然会报错
		    	try {
					SmsTxSend.sendSms(phone, send);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	}
		
}
